#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //初始化
    this->setWindowTitle("客户端");
    QLabel *textLabel = new QLabel;
    textLabel->setText("连接状态：");
    statusConnect = new QLabel;
    statusConnect->setPixmap(QPixmap(":/res/linkN.png").scaled(20, 20));
    ui->statusbar->addWidget(textLabel);
    ui->statusbar->addWidget(statusConnect);
    ui->disconnectBtn->setDisabled(true);

    //连接服务器按钮
    connect(ui->connectBtn, &QPushButton::clicked, [=](){
        if(ui->hostName->text() == "" || ui->port->text() == ""){
            QMessageBox::warning(this, "警告", "服务器地址和端口不能为空!");
        }else{
            //禁止多次点击连接按钮
            ui->connectBtn->setDisabled(true);
            ui->connectBtn->setText("连接中");

            //1.创建套接字对象
            socket = new QTcpSocket(this);
            //2.连接服务器
            socket->connectToHost(QHostAddress(ui->hostName->text()), ui->port->text().toUShort());

        }

        //监听连接服务器 成功信号
        connect(this->socket, &QTcpSocket::connected, [=](){
            ui->recordBox->append("---连接服务器成功---");
            this->statusConnect->setPixmap(QPixmap(":/res/linkY.png").scaled(20, 20));
            this->flag = true;

            //禁用连接按钮 启用断开按钮
            ui->connectBtn->setDisabled(true);
            ui->disconnectBtn->setDisabled(false);
        });
        //监听 连接断开信号
        connect(this->socket, &QTcpSocket::disconnected, [=](){
            qDebug() << "监听到断开连接";
            ui->recordBox->append("---与服务器断开连接---");
            ui->connectBtn->setText("连接服务器");

            //启用连接按钮 警用断开按钮
            ui->connectBtn->setDisabled(false);
            ui->disconnectBtn->setDisabled(true);
            this->flag = false;

            //手动释放 和绑定父对象
            this->socket->close();
            this->socket->deleteLater();
            statusConnect->setPixmap(QPixmap(":/res/linkN.png").scaled(20, 20));
        });
        //监听 收到服务器数据
        connect(this->socket, &QTcpSocket::readyRead, [=](){
            QByteArray data = this->socket->readAll();
            ui->recordBox->append("服务器说：" + data);
        });
    });

    //发送按钮
    connect(ui->sendMsg, &QPushButton::clicked, this, [=](){
        if(this->flag == false){
            QMessageBox::warning(this, "警告", "连接断开!");
        }else if(ui->msgBox->toPlainText() == ""){
            QMessageBox::warning(this, "警告", "发送内容为空!");
        }else{
            //3.向服务器发送数据
            QString str = ui->msgBox->toPlainText();
            socket->write(str.toUtf8());
            ui->recordBox->append("客户端说(我)：" + str);
            ui->msgBox->setPlainText("");
        }
    });

    //断开连接按钮
    connect(ui->disconnectBtn, &QPushButton::clicked, [=](){
        if(this->flag == true){
            this->socket->close();
            this->socket->deleteLater();
            this->flag = false;

        }else{
            qDebug() << "服务器未启动！";
        }
    });

}

MainWindow::~MainWindow()
{
    delete ui;
}
